<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/9/14
 * Time: 下午5:21
 */

namespace app\admin\logic;


use extend\Functions;
use extend\STATUS_CODE;
use const Grpc\STATUS_ABORTED;

class CollectionPoint
{
    /**
     * @purpose 添加集合点(支持批量添加)
     * @param array $data
     * @return array
     * @throws \Exception
     * @throws \think\exception\PDOException
     */
    public static function add (array $data): array
    {
        if (!isset($data['address_info']['province']['name']) ||
            empty($data['address_info']['province']['name'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少省市信息!'
            );
        }
        if (!isset($data['address_info']['city']['name']) ||
            empty($data['address_info']['city']['name'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少城市信息!'
            );
        }
        if (!isset($data['address_info']['county']['name']) ||
            empty($data['address_info']['county']['name'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少区县信息!'
            );
        }
        // 构造批量添加数据
        $insert_data = [];
        foreach ($data['collection_point'] as $info) {
            if (!isset($info['title']) || empty($info['title'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '请至少添加一条上车点!'
                );
            }
            if (!isset($info['address']) || empty($info['address'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '请至少添加一条详细地址!'
                );
            }
            array_push($insert_data, [
                'province_id'   => $data['address_info']['province']['id'],
                'city_id'       => $data['address_info']['city']['id'],
                'county_id'     => $data['address_info']['county']['id'],
                'province_name' => $data['address_info']['province']['name'],
                'city_name'     => $data['address_info']['city']['name'],
                'county_name'   => $data['address_info']['county']['name'],
                'province_code' => $data['address_info']['province']['code'],
                'city_code'     => $data['address_info']['city']['code'],
                'county_code'   => $data['address_info']['county']['code'],
                'address'       => $info['address'],
                'lng'           => $info['lng'],
                'lat'           => $info['lat'],
                'title'         => $info['title'],
            ]);
        }
        $model          = new \app\model\CollectionPoint();
        $model->startTrans();
        $result         = $model
            ->isUpdate(false)
            ->saveAll($insert_data);
        if ($result->count() !== count($data['collection_point'])) {
            $model->rollback();
            return Functions::result(
                STATUS_CODE::CREATE_FAIL,
                '添加失败'
            );
        }
        $model->commit();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            '添加成功!'
        );
    }

    /**
     * @purpose 通过ID修改上车点信息
     * @param array $data
     * @return array
     */
    public static function edit (array $data): array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
              STATUS_CODE::PARAMETER_ERROR,
              '缺少参数ID!'
            );
        }
        if (!isset($data['address']) || empty($data['address']) ) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少详细地址!'
            );
        }
        if (!isset($data['lng']) || empty($data['lng'])){
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少经度!'
            );
        }
        if (!isset($data['lat']) || empty($data['lat'])){
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少纬度!'
            );
        }
        if (!isset($data['title']) || empty($data['title']) ) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少集合点名称!'
            );
        }
        $model  = new \app\model\CollectionPoint();
        $result = $model
            ->isUpdate(true)
            ->save($data,['id' => (int) $data['id']]);
        if (!$result) {
            return Functions::result(
              STATUS_CODE::FAIL,
              '更新失败!'
            );
        }
        return Functions::result(
            STATUS_CODE::SUCCESS,
            '更新成功!'
        );
    }

    /**
     * @purpose 根据ID删除上车地点(支持批量删除)
     * @param array $ids
     * @return array
     * @throws \Exception
     */
    public static function delete (array $ids): array
    {
        if (empty($ids)) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少参数ID!'
            );
        }
        $model = new \app\model\CollectionPoint();
        $result = $model
            ->destroy($ids);
        if (!$result) {
            return Functions::result(
              STATUS_CODE::FAIL,
              '删除失败!'
            );
        }
        return Functions::result(
            STATUS_CODE::SUCCESS,
            '删除成功!'
        );
    }

    /**
     * @purpose 选择性获取上车地点列表
     * @param string $province_name
     * @param string $city_name
     * @param string $county_name
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function items (
        string $province_name ,string $city_name,string $county_name,
        int $page = 1, int $size = 50): array
    {
        $model = new \app\model\CollectionPoint();
        $fields = 'id,province_name,city_name,county_name,address,title,lng,lat';
        // 没有区域选择
        if ($province_name == '' && $city_name == '' && $county_name == '') {
            $result = $model
                ->field($fields)
                ->page($page, $size)
                ->order('create_at desc')
                ->select();
            $where = ['club_id' => 0];
            $total = $model->total($where);
            return Functions::result(
                STATUS_CODE::SUCCESS,
                [
                    'total' => $total,
                    'list'  => $result
                ]
            );
        }
        // 获取相关省份的上车点
        if ($province_name !=='' && $city_name == '') {
            $where = [
              'province_name' => $province_name
            ];
            $result = $model
                ->where($where)
                ->field($fields)
                ->page($page,$size)
                ->order('create_at desc')
                ->select();
            $total = $model->total($where);
            return Functions::result(
                STATUS_CODE::SUCCESS,
                [
                    'total' => $total,
                    'list'  => $result
                ]
            );
        }
        // 获取省份 & 城市相关的上车点
        if ($province_name !=='' && $city_name !== '' && $county_name == '') {
            $where = [
                'province_name' => $province_name,
                'city_name'     => $city_name
            ];
            $result = $model
                ->where($where)
                ->field($fields)
                ->page($page,$size)
                ->order('create_at desc')
                ->select();
            $total = $model->total($where);
            return Functions::result(
                STATUS_CODE::SUCCESS,
                [
                    'total' => $total,
                    'list'  => $result
                ]
            );
        }
        // 获取省份 & 城市 & 区/县相关的上车点
        if ($province_name !=='' && $city_name !== '' && $county_name !== '') {
            $where = [
                'province_name' => $province_name,
                'city_name'     => $city_name,
                'county_name'   => $county_name
            ];
            $result = $model
                ->where($where)
                ->field($fields)
                ->page($page,$size)
                ->order('create_at desc')
                ->select();
            $total = $model->total($where);
            return Functions::result(
                STATUS_CODE::SUCCESS,
                [
                    'total' => $total,
                    'list'  => $result
                ]
            );
        }
    }

    /**
     * @purpose 通过市级ID获取上车集合点
     * @param int $city_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function itemsByCity (int $city_id): array
    {
        $model          = new \app\model\CollectionPoint();
        $list           = $model
            ->where('city_id', $city_id)
            ->field('id,province_id,city_id,county_id,province_code,city_code,county_code,province_name,city_name,county_name,address,title,lng,lat')
            ->order('create_at desc')
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $list
        );
    }
}